package edu.sjtu.grid.workflow.schedule;

import edu.sjtu.grid.workflow.Util;
import edu.sjtu.grid.workflow.facts.Facts;
import edu.sjtu.grid.workflow.facts.Job;
import edu.sjtu.grid.workflow.facts.Mapping;
import edu.sjtu.grid.workflow.facts.PC;
import edu.sjtu.grid.workflow.iterator.UpwardRankIterator;

public class HEFTSchedule extends ScheduleBase{

	public HEFTSchedule(Facts facts) {
		super(facts, ScheduleType.HEFT);
	}
	
	@Override
	protected void doSchedule()
	{	
		UpwardRankIterator i = this.getFacts().getAdag().getUpwardRankIterator();
		double fromTime = 0;
		while(i.hasNext())
		{
			Job job = (Job)i.next();
			Mapping earlistFinishMapping = null;
			for(PC pc : this.getFacts().getPcs().getPcList())
			{
				Mapping mapping = this.getRun().getEarlistMapping(pc, job, fromTime);
				if(earlistFinishMapping == null 
						|| Util.isSmall(mapping.getToTime(), earlistFinishMapping.getToTime()))
					earlistFinishMapping = mapping;
			}
			assert(earlistFinishMapping != null);
			this.getRun().confirmMapping(earlistFinishMapping);
		}
		this.setResult(true);
	}

}
